package com.iwomy.envpro.platform.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iwomy.envpro.common.core.util.R;
import com.iwomy.envpro.common.log.annotation.SysLog;
import com.iwomy.envpro.platform.VO.AirAlarmSettingVO;
import com.iwomy.envpro.platform.VO.AirOverThresholdVO;
import com.iwomy.envpro.platform.dto.AirAlarmSettingDTO;
import com.iwomy.envpro.platform.dto.AirOverThresholdDTO;
import com.iwomy.envpro.platform.entity.AirAlarmSettingEntity;
import com.iwomy.envpro.platform.entity.AirOverThresholdEntity;
import com.iwomy.envpro.platform.service.AirAlarmSettingService;
import com.iwomy.envpro.platform.service.AirOverThresholdService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.iwomy.envpro.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 报警设置表
 *
 * @author pig
 * @date 2023-08-29 14:59:36
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/airAlarmSetting" )
@Tag(description = "airAlarmSetting" , name = "报警设置表管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class AirAlarmSettingController {

    private final  AirAlarmSettingService airAlarmSettingService;
    private final AirOverThresholdService airOverThresholdService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param airAlarmSetting 报警设置表
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('platform_airAlarmSetting_view')" )
    public R getAirAlarmSettingPage(@ParameterObject Page page,@ParameterObject AirAlarmSettingEntity airAlarmSetting) {
        LambdaQueryWrapper<AirAlarmSettingEntity> wrapper = Wrappers.lambdaQuery(airAlarmSetting);
        return R.ok(airAlarmSettingService.page(page, wrapper));
    }


    /**
     * 通过id查询报警设置表
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('platform_airAlarmSetting_view')" )
    public R getById(@PathVariable("id" ) Long id) {
		AirAlarmSettingEntity airAlarmSetting = airAlarmSettingService.getById(id);
		AirAlarmSettingVO airAlarmSettingVO = BeanUtil.copyProperties(airAlarmSetting, AirAlarmSettingVO.class);
		List<AirOverThresholdEntity> list = airOverThresholdService.list(Wrappers.<AirOverThresholdEntity>lambdaQuery().eq(AirOverThresholdEntity::getAlarmSettingId, airAlarmSetting.getId()));
		List<AirOverThresholdVO> airOverThresholdVOS = BeanUtil.copyToList(list, AirOverThresholdVO.class);
		airAlarmSettingVO.setAirOverThresholdList(airOverThresholdVOS);
		return R.ok(airAlarmSettingVO);
    }

    /**
     * 新增报警设置表
     * @param airAlarmSetting 报警设置表
     * @return R
     */
    @Operation(summary = "新增报警设置表" , description = "新增报警设置表" )
    @SysLog("新增报警设置表" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('platform_airAlarmSetting_add')" )
    public R save(@RequestBody AirAlarmSettingEntity airAlarmSetting) {
        return R.ok(airAlarmSettingService.save(airAlarmSetting));
    }

    /**
     * 修改报警设置表
     * @param airAlarmSetting 报警设置表
     * @return R
     */
    @Operation(summary = "修改报警设置表" , description = "修改报警设置表" )
    @SysLog("修改报警设置表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('platform_airAlarmSetting_edit')" )
    public R updateById(@RequestBody AirAlarmSettingDTO airAlarmSetting) {
		AirAlarmSettingEntity airAlarmSettingEntity = BeanUtil.copyProperties(airAlarmSetting, AirAlarmSettingEntity.class);

		List<AirOverThresholdDTO> airOverThresholdList = airAlarmSetting.getAirOverThresholdList();

		List<AirOverThresholdEntity> airOverThresholdEntities = BeanUtil.copyToList(airOverThresholdList, AirOverThresholdEntity.class);

		airOverThresholdService.updateBatchById(airOverThresholdEntities);

		return R.ok(airAlarmSettingService.updateById(airAlarmSettingEntity));
    }

    /**
     * 通过id删除报警设置表
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除报警设置表" , description = "通过id删除报警设置表" )
    @SysLog("通过id删除报警设置表" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('platform_airAlarmSetting_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(airAlarmSettingService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param airAlarmSetting 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('platform_airAlarmSetting_export')" )
    public List<AirAlarmSettingEntity> export(AirAlarmSettingEntity airAlarmSetting) {
        return airAlarmSettingService.list(Wrappers.query(airAlarmSetting));
    }
}